<div class="rich-prize-pic">
	<div class="rich-prize-pic-box">
		<img src="{$activity.pic}" />
	</div>
</div>
<div class="rich-action">
	<empty name="hostInfo">
		<!--如果没有发起活动-->
		<eq name="userInfo.subscribe" value="1">
			<div class="rich-info"><span>{$userInfo.nickname}</span>，您已关注公众账号，获得初始红包 <span>{$activity.defaultMoney}</span> 元。快点发起活动一起领取红包吧！</div>
			<else />
			<div class="rich-info">您还没有关注公众账号，关注后就可获得 <span>{$activity.defaultMoney}</span> 元的初始红包。还等什么，快点关注吧！</div>
		</eq>
		<a id="bt-host" class="button button-block button-rounded button-flat-royal">我要发起活动，抢红包</a>
		<a id="bt-share" class="button button-block button-rounded button-flat-caution">分享给朋友，一起抢红包</a>
	<else />
		<!--如果已经发起活动-->
		<if condition="$hostInfo['diff'] gt 0">
			<div class="rich-info"><span>{$userInfo.nickname}</span>，您当前的红包总额为 <span>{$hostInfo.money}</span> 元，还差 <span>{$hostInfo.diff}</span> 元就可以领取了，赶快邀请好友帮您实现梦想吧！</div>
			<a id="bt-play" class="button button-block button-rounded button-flat-royal">邀请朋友，帮我抽红包</a>
		<else />
			<div class="rich-info"><span>{$userInfo.nickname}</span>，您当前的红包总额为 <span>{$hostInfo.money}</span> 元，已经完成目标，赶快领取吧！</div>
			<a id="bt-register" class="button button-block button-rounded button-flat-royal">登记信息，领取红包</a>
			<div class="rich-register rich-process">
				<empty name="registerInfo">
				<form name="form-register">
					<input type="text" id="name" name="name" value="" placeholder="请输入姓名">
					<input type="tel" id="tel" name="tel" value="" placeholder="请输入电话">
					<a id="bt-submit" class="button button-block button-rounded button-flat-primary">提交信息</a>
				</form>
				<else />
				<h3>您已经领取红包</h3>
				<p><a href="__MODULE__/DisShopRedPakcet/list/type/1/sid/{$activity.cid}" class="button button-block button-rounded button-flat-royal">查看红包</a></p>
				<h3>您的登记信息如下：</h3>
				<ul>
					<li>姓名: {$registerInfo.name}</li>
					<li>电话: {$registerInfo.tel}</li>
				</ul>
				</empty>
			</div>
		</if>
		<a id="bt-view" class="button button-block button-rounded button-flat-caution">查看我的红包明细</a>
	</empty>
</div>

<notempty name="hostInfo">
<div id="rich-detail" class="rich-process">
	<h3><span>总额: <b id="rich-money-total">{$hostInfo.money}</b></span>红包明细</h3>
	<p>正在加载……</p>
	<ol id="rich-detail-list">
	</ol>
</div>
</notempty>

<script type="text/javascript">
	$(function() {
		// 提交登记
		$("#bt-submit").click(function(){
			if($("#name").val()==""){
				simapp.toast("姓名不能为空");
				return ;
			}
			if($("#tel").val()==""){
				simapp.toast("电话不能为空");
				return ;
			}
			var othis = $(this);
			$.ajax({
				type:'POST',
				url:'__CONTROLLER__/register?debug',
				data:{
					cid:userInfo.cid,
					aid:userInfo.aid,
					uid:userInfo.id,
					oid:userInfo.openid,
					hid:hostInfo.id,
					name:$("#name").val(),
					tel:$("#tel").val(),
				},
				beforeSend: function() {
					othis.attr("disabled", true);
					simapp.toast('正在提交……');
				},
				success: function(response) {
					if (response.state && response.state == 1) {
						simapp.toast(response.msg);
						setTimeout(function(){
						    location.href="__MODULE__/DisShopRedPakcet/list/type/1/sid/{$activity.cid}";
						},3000)
					} else {
						simapp.toast(response.msg);
					}
				},
				error: function(xhr, errorType, error) {
					simapp.toast("网络错误，请重新尝试！");
				},
				complete: function() {
					othis.removeAttr('disabled');
				}
			});
		});
		
		// 查看基金明细
		$("#bt-view").click(function() {
			$.ajax({
				type: "POST",
				url: "__CONTROLLER__/detail?debug",
				data: {
					cid: userInfo.cid,
					aid: userInfo.aid,
					hid: hostInfo.id
				},
				beforeSend: function() {
					$("#rich-detail p").show();
					$("#rich-detail ol").hide();
				},
				success: function(response) {
					if (response.state && response.state == 1) {
						simapp.toast(response.msg);
						
						var list = [];
						var play;
						var total = 0;
						if(activity.defaultMoney && activity.defaultMoney > 0 ){ 
							total += parseInt(activity.defaultMoney);
							list.push("<li><span>" + activity.defaultMoney + "</span>初始红包</li>");
						}
						if(response.data){
							var data = response.data;
							for (var i = 0, l = data.length; i < l; i++) {
								play = data[i];
								total += parseInt(play.money);
								total = Math.max(total,0);
								list.push("<li><span>" + play.money + "</span>" + (play.nickname||'匿名朋友') + "</li>");
							}
						}
						
						$("#rich-detail-list").html(list.join(''));
						$("#rich-money-total").html(Math.max(total,0));
					} else {
						simapp.toast(response.msg);
					}
				},
				error: function(xhr, errorType, error) {
					simapp.toast("网络错误，请重试！");
				},
				complete: function() {
					$("#rich-detail p").hide();
					$("#rich-detail ol").show();
				}
			});
		});

		// 发起活动
		$('#bt-host').click(function() {
			var othis = $(this);
			if (userInfo && userInfo.subscribe) {
				simapp.toast('正在申请发起活动，请稍候……');
				$(this).attr('disabled', true);
				$.ajax({
					type: 'POST',
					url: '__CONTROLLER__/host?debug',
					data: {
						cid: userInfo.cid,
						aid: userInfo.aid,
						uid: userInfo.id
					},
					beforeSend: function() {
						othis.attr("disabled", true);
					},
					success: function(response) {
						if (response.state && response.state == 1) {
							simapp.toast(response.msg);
							location.href = activity.shareUrl;
						} else {
							simapp.toast(response.msg);
						}
					},
					error: function(xhr, errorType, error) {
						simapp.toast("请重新打开页面尝试！");
					},
					complete: function() {
						othis.removeAttr('disabled');
					}
				});
			} else {
				simapp.tips('关注公众平台后才能发起活动哦，关注后还能获得 <b>' + activity.defaultMoney + '</b> 元的初始红包呢！还等什么，快点关注吧！<br />关注方法：点击右上角->查看公众号->关注');
			}
		});
		// 分享给朋友
		$('#bt-share').click(function() {
			simapp.tips("好事哪能独享，快点发给朋友！");
		});
		// 邀请朋友，帮我抽红包
		$('#bt-play').click(function() {
			simapp.tips("让您的小伙伴们一起为您抽红包吧！")
		});
		// 登记信息
		$('#bt-register').click(function(){
			$(".rich-register").toggle();
		});
	});
</script>

